//
//  PersistenceCenter.m
//  drawacartoon
//
//  Created by Ji Hu on 12-9-26.
//  Copyright (c) 2012年 爱辅导. All rights reserved.
//

#import "PersistenceCenter.h"

static PersistenceCenter *defaultcenter = nil;

@interface PersistenceCenter (Private)


@end

@implementation PersistenceCenter{
    NSString *_persistenceFilePath;
}

+(PersistenceCenter *)defaultCenter{
    
    if (defaultcenter == nil) {
        defaultcenter = [[PersistenceCenter alloc] initWithPersistenceName:@"defaultPersistenceFile"];
    }
    
    return defaultcenter;
}

-(void)dealloc{
    [_persistenceFilePath release];
    [super dealloc];
}

-(id)initWithPersistenceName:(NSString *)name{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *persistenceFilePath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,name];
    
    self = [super initWithContentsOfFile:persistenceFilePath];
    if (self) {
       
        _persistenceFilePath = [persistenceFilePath retain];
    }
    
    return self;
}

-(void)savePersistence{
    [self writeToFile:_persistenceFilePath atomically:YES];
}

@end
